package cn.org.queue.send;

import cn.org.queue.manage.RocketQueueManager;
import com.alibaba.fastjson.JSONObject;
import lombok.NoArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.rocketmq.client.producer.SendResult;
import org.apache.rocketmq.common.message.Message;


/**
 * @Package: cn.org.queue.send
 * @ClassName: RocketProducerMessage
 * @Description:
 * @Author: mac-pro
 * @CreateDate: 2019/2/18 下午4:57
 * @Version: 1.0
 */
@Slf4j
@NoArgsConstructor
public class RocketProducerMessage extends RocketQueueManager {
    public void sendMessage(String topic,String tag,String keys, String msgTplId, String system, Object payload) {
        byte[] bytes = JSONObject.toJSONBytes(payload);
        SendResult publish = publish(topic, tag, keys, bytes);
        log.info("即时消息发送成功.状态为:{},topic={},payload={}",JSONObject.toJSONString(publish), topic,payload);
    }


    public SendResult publish(String topic,String tag,String key, byte[] payload){
        /**
         *  topic
         *  tag
         *  key
         *  payload
         */
        try {
            Message msg = new Message(topic, tag, key, payload);
            SendResult sendResult = producer.send(msg);
            return sendResult;
        } catch (Exception e) {
            log.error("rocket send msg error",e);
        }
        return null;
    }
}
